package org.zoomdev.zoom.dao.adapters.parameter;

import org.zoomdev.zoom.common.caster.ValueCaster;
import org.zoomdev.zoom.dao.adapters.ParameterAdapter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 先转下格式，然后在调用setObject
 * Map/Set等集合形式需要先转成json格式才行
 */
public class CasterStatementAdapter implements ParameterAdapter {

    private final ValueCaster valueCaster;

    public CasterStatementAdapter(ValueCaster valueCaster) {
        this.valueCaster = valueCaster;
    }

    @Override
    public void setNonnullParameter(PreparedStatement ps, int index, Object data) throws SQLException {
        ps.setObject(index, valueCaster.to(data));
    }
}
